// ==UserScript==
// @name        ggSpamFree
// @namespace   http://code.google.com/p/ggspamfree/
// @version     "1.2.7.9"
// @include     http://groups.google.com/group/*/topics*
// @include     http://groups.google.tld/group/*/topics*
// @unwrap
// @description Google Groups spam filter
// @copyright   (c)2009, VK -- (c)2010, Nick
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @contributor Evertjan
// @contributor Francesco S. Carta
// @contributor Jorge
// ==/UserScript==





void((function(){

  var UserScript = {
    name: 'ggSpamFree',
    namespace: 'http://code.google.com/p/ggspamfree/',
    version: "1.2.7.9",
    include: 'http://groups.google.tld/group/*/topics*',
    unwrap: true,
    description: 'Google Groups spam filter',
    copyright: '(c)2010, Nick',
    license: {
      type: 'GPL version 3 or any later version',
      url: 'http://www.gnu.org/copyleft/gpl.html',
      URL: 'http://www.gnu.org/copyleft/gpl.html'
    },
    contributor: ['Evertjan','Francesco S. Carta','Jorge','VK']
  }


  var stopwords = [
      '~~|##|\\!\\!\\!|\\$\\$\\$'

,'abilify'
,'acai'
,'accupril'
,'accutane'
,'aceon'
,'acomplia'
,'actos'
,'acyclovir'
,'adidas'
,'adsense'
,'adult'
,'air.jordan'
,'alprazolam'
,'ambien'
,'amoxicilline?'
,'anaprox'
,'armani'
,'atrovent'
,'audigier'
,'avelox'
,'(b|h)ollywood'
,'brandtrade\d+'
,'butalbital'
,'cash.*checks'
,'carisoprodol'
,'casinos?'
,'chloroquine'
,'cialis'
,'cipro'
,'citolapram'
,'cntrade\d*'
,'codeine'
,'combivent'
,'debt.(consolidation|settlement)'
,'desyrel'
,'detrol'
,'diamox'
,'diazepam'
,'diflucan'
,'discount'
,'dramamine'
,'eodolac'
,'erythromycin'
,'exotic.skin'
,'fedex'
,'for.free'
,'fioricet'
,'fjrjtrade'
,'free.downloads'
,'free.online'
,'(get|make|earn).\$\d+'
,'gucci'
,'handbags'
,'.ollywood.actress'
,'hydrea'
,'hydrocodone'
,'insurance'
,'jerseys?'
,'jimmy.choo'
,'\/job\/'
,'job.id'
,'kamagra'
,'levitra'
,'life.insurance'
,'lortab'
,'low.price'
,'lunesta'
,'microzide'
,'minocycline'
,'money.order'
,'nexium'
,'nude'
,'over.the.counter'
,'oxycodone'
,'oxycontin'
,'paypal'
,'phentermine'
,'physician'
,'pills'
,'prazosin'
,'prilosec'
,'revia'
,'revenue'
,'sexy?'
,'shoestrade'
,'sightseeing'
,'singulair'
,'soma'
,'solutions?.manuals?'
,'tagamet'
,'terrorism'
,'tits'
,'topamax'
,'toradol'
,'tramadol'
,'tv.channels'
,'ultram'
,'vermox'
,'underwear'
,'valium'
,'versace'
,'viagra'
,'vicodin'
,'wellbutrin'
,'world.cup'
,'xanax'
,'xenical'
,'zithromax'
,'zolpidem'
,'zovirax'
,'zyban'
,'zyprexa'
,'zyvox'
,'pharmac(y|ies)'
,'p[re]{2}scription'
,'ad{1,2}er{1,2}al{1,2}'
,'(buy|purchase).(cheap|(not.|in)expensive|on.?line)'
,'(\\w*day|fast|overnight).(delivery|shipping)'
,'(earn(ing)?|mak(e|ing)).money'
,'(hot.|whole)sale'
,'(dance.*){3,}'
,'(jobs.*){3,}'
,'(girls.*){3,}'
,'(shoe.*){3,}'

    ];




  var whitelist = [];

  go();




  function ce (tag) {
   return document.createElement(tag);
  }

  function checkNodeMethod (methodName) {
    return typeof document.body[methodName] == 'function';
  }



  function go () {

    var WORD_START = '(\\W|^)', WORD_END = '(\\W|$)';

    var filter = new RegExp(WORD_START
    + stopwords.join(WORD_END + '|' + WORD_START)
    + WORD_END, 'i');

    var unfilter = new RegExp(WORD_START
    + whitelist.join(WORD_END + '|' + WORD_START)
    + WORD_END, 'i');





    var CONTENT_DIV_CLASS = 'maincontoutboxatt';

    var i = 0;
    var found = false;



    var div = document.getElementsByTagName('DIV');
    for (i=0; i<div.length; i++) {
      if (div[i].className != CONTENT_DIV_CLASS) continue;
      div = div[i];
      found = true;
      break;
    }






    if (!found) {
      throw new Error(
        UserScript.name.concat(' ', UserScript.version, ': ',
          'Div [@class="', CONTENT_DIV_CLASS, '"] not found.')
      );
      return;
    }





    var tables = div.getElementsByTagName('TABLE');
    if (tables.length == 0) {
      throw new Error(
        UserScript.name.concat(' ', UserScript.version, ': ',
          'No tables found in the content div')
      );
      return;
    }

    var summary = (tables.length > 1);



    var posting = summary ? tables : tables[0].tBodies[0].rows;



    var parent = summary ? div : tables[0].tBodies[0];

    var spamCount=0;
    var spamContainer = document.createElement('table');
    spamContainer.style.display='none';



    if (!summary)
      spamContainer.appendChild(posting[0].cloneNode(true));



    console.dir(filter);
    for (i = summary ? 0 : 2; i<posting.length; i++) {
      var p = summary? posting[i].rows[0] : posting[i];
      var text = p.cells[1].textContent || p.cells[1].innerText;

      if ( ( text.toUpperCase() == text || filter.test(text))
          && (whitelist.length==0 || !unfilter.test(text))) {
        var s = posting[i].cloneNode(true);
        s.style.backgroundColor=['white','#f8f8f8'][spamCount++%2];
        spamContainer.appendChild(s);
        posting[i].style.display='none';
      }
    }
    var c = 0;
    for (i = summary ? 0 : 2; i<posting.length; i++) {
      if (posting[i].style.display!='none')
        posting[i].style.backgroundColor=['white','#f8f8f8'][c++%2];
    }

    if (spamCount) {
      var spamOuter = ce('div');
      var toggle = ce('input');
      toggle.type='button';
      toggle.value='Show spam';
      toggle.style.margin='2em';
      toggle.onclick= function(){
        var s=this.nextSibling.style;
        if (s.display=='none') {
          s.display = 'block'; this.value = 'Hide spam';
        } else {
          s.display = 'none'; this.value = 'Show spam';
        }
      }
      spamOuter.appendChild(toggle);
      spamOuter.appendChild(spamContainer);
      tables[0].parentNode.appendChild(spamOuter);
    }





  }

})())
